<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="源MAC" prop="ethSrc">
        <el-input v-model="dataForm.ethSrc" placeholder="源MAC"></el-input>
      </el-form-item>
      <el-form-item label="目标MAC" prop="ethDst">
        <el-input v-model="dataForm.ethDst" placeholder="目标MAC"></el-input>
      </el-form-item>
      <el-form-item label="源IPV4" prop="ipv4Src">
        <el-input v-model="dataForm.ipv4Src" placeholder="源IPV4"></el-input>
      </el-form-item>
      <el-form-item label="目标IPV4" prop="ipv4Dst">
        <el-input v-model="dataForm.ipv4Dst" placeholder="目标IPV4"></el-input>
      </el-form-item>
      <el-form-item label="源端口" prop="portSrc">
        <el-input v-model="dataForm.portSrc" placeholder="源端口"></el-input>
      </el-form-item>
      <el-form-item label="目标端口" prop="portDst">
        <el-input v-model="dataForm.portDst" placeholder="目标端口"></el-input>
      </el-form-item>
      <el-form-item label="输出端口号" prop="outputPort">
        <el-input v-model="dataForm.outputPort" placeholder="输出端口号"></el-input>
      </el-form-item>
      <el-form-item label="流表ID" prop="tableId">
        <el-input v-model="dataForm.tableId" placeholder="流表ID"></el-input>
      </el-form-item>
      <el-form-item label="流表优先级" prop="priority">
        <el-input v-model="dataForm.priority" placeholder="流表优先级"></el-input>
      </el-form-item>
      <el-form-item label="软超时时间" prop="idleTimeout">
        <el-input v-model="dataForm.idleTimeout" placeholder="软超时时间"></el-input>
      </el-form-item>
      <el-form-item label="硬超时时间" prop="hardTimeout">
        <el-input v-model="dataForm.hardTimeout" placeholder="硬超时时间"></el-input>
      </el-form-item>
      <el-form-item label="操作类型" prop="flowModCMD">
        <el-radio-group v-model="dataForm.flowModCMD" >
          <el-radio :label="0">添加</el-radio>
          <el-radio :label="1">修改</el-radio>
          <el-radio :label="2">删除</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          dpid: '',
          ethSrc: '',
          ethDst: '',
          ipv4Src: '',
          ipv4Dst: '',
          portSrc: '',
          portDst: '',
          outputPort: '',
          tableId: '',
          priority: '',
          idleTimeout: '',
          hardTimeout: '',
          flowModCMD: ''

        },
        dataRule: {
          ethSrc: [
            { required: true, message: '源MAC不能为空', trigger: 'blur' }
          ],
          ethDst: [
            { required: true, message: '目标MAC不能为空', trigger: 'blur' }
          ],
          ipv4Src: [
            { required: true, message: '源IPV4不能为空', trigger: 'blur' }
          ],
          ipv4Dst: [
            { required: true, message: '目标IPV4不能为空', trigger: 'blur' }
          ],
          portSrc: [
            { required: true, message: '源端口不能为空', trigger: 'blur' }
          ],
          portDst: [
            { required: true, message: '目标端口不能为空', trigger: 'blur' }
          ],
          outputPort: [
            { required: true, message: '输出端口不能为空', trigger: 'blur' }
          ],
          tableId: [
            { required: true, message: '流表ID能为空', trigger: 'blur' }
          ],
          priority: [
            { required: true, message: '优先级不能为空', trigger: 'blur' }
          ],
          idleTimeout: [
            { required: true, message: '软超时时间不能为空', trigger: 'blur' }
          ],
          hardTimeout: [
            { required: true, message: '硬超时时间不能为空', trigger: 'blur' }
          ],
          flowModCMD: [
            { required: true, message: '操作类型未选择', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (dpid) {
        this.dataForm.dpid = dpid || ''
        this.visible = true
        /*this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dpid) {
            this.$http({
              url: this.$http.adornEndogenousUrl(`/generator/switch/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.switch.name
                this.dataForm.status = data.switch.status
                this.dataForm.controller = data.switch.controller
                this.dataForm.controllerStatus = data.switch.controllerStatus
              }
            })
          }
        })*/
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornEndogenousUrl(`/generator/switch/flowTable`),
              method: 'post',
              data: this.$http.adornData({
                'dpid': this.dataForm.dpid || undefined,
                'ethSrc': this.dataForm.ethSrc,
                'ethDst': this.dataForm.ethDst,
                'ipv4Src': this.dataForm.ipv4Src,
                'ipv4Dst': this.dataForm.ipv4Dst,
                'portSrc': this.dataForm.portSrc,
                'portDst': this.dataForm.portDst,
                'outputPort': this.dataForm.outputPort,
                'tableId': this.dataForm.tableId,
                'priority': this.dataForm.priority,
                'idleTimeout': this.dataForm.idleTimeout,
                'hardTimeout': this.dataForm.hardTimeout,
                'flowModCMD': this.dataForm.flowModCMD
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
